package net.system.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.core.base.dao.CommonDAO;
import net.core.common.DateFormater;
import net.core.common.StrUtils;
import net.core.service.encrype.MD5;
import net.system.base.bean.SSysuser;
import net.system.base.bean.SSysuserExample;
import net.system.base.module.Column;
import net.system.base.module.Table;
import net.system.common.BeanUtils;
import net.system.sys.dao.SSysuserDAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private CommonDAO commonDAO;
	@Autowired
	private SSysuserDAO dao;

	@Autowired
	private ICommService commService;

	@Override
	public List<SSysuser> selectUser(HashMap reqMap) {
		SSysuserExample example = new SSysuserExample();
		SSysuserExample.Criteria c = example.createCriteria();
		String type = StrUtils.getNotNullStrValue(reqMap.get("type"));
		if("referal".equals(type)){
			//下线查看
		}else if("manage".equals(type)){
			// 管理模式
		}
		// c.andRecStatusEqualTo("1");
		List<SSysuser> list = dao.selectByExample(example);
		return list;
	}

	@Override
	public SSysuser getUser(String recId) {
		return dao.selectByPrimaryKey(recId);
	}

	@Override
	public void insert(HashMap map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(HashMap map) {
		
		SSysuser record = new SSysuser();
		record.setRecId( StrUtils.getNotNullStrValue(map.get("recId")));
		record.setSuAlipay( StrUtils.getNotNullStrValue(map.get("suAlipay")));
		record.setSuName( StrUtils.getNotNullStrValue(map.get("suName")));
		record.setSuEmail( StrUtils.getNotNullStrValue(map.get("suEmail")));
		record.setSuQq( StrUtils.getNotNullStrValue(map.get("suQq")));
		
		dao.updateByPrimaryKeySelective(record);

	}

	private List<Column> setColumnsValue(List<Column> columns, Map reqMap) {
		if (null != columns && columns.size() > 0) {
			for (Column column : columns) {
				String sVal = "";
				if (column.isSys()) {
					if (column.getName().equals("REC_ID")) {
						sVal = commService.getSeq("user");
					} else if (column.getName().equals("REC_PERSON")) {
						sVal = StrUtils.getNotNullStrValue(reqMap.get("user"));
						if ("".equals(sVal)) {
							sVal = "sysdef";
						}
					} else if (column.getName().equals("REC_TIME")) {
						sVal = DateFormater.getNowTime();
					}
				} else {
					// 按照字符串指定长度截取
					if (column.getType().equals("varchar")
							&& sVal.length() > Integer.parseInt(column.getLength())) {
						sVal = sVal.substring(0, Integer.parseInt(column.getLength()));
					}
				}
				column.setValue(sVal);
			}
		}
		return columns;
	}

	public Table initUser() {
		Table user = new Table("SSysuser", "S_SYSUSER", "用户信息表", new ArrayList<Column>());
		user.addColumn(new Column("suQq", "SU_QQ", "20", "varchar", false, ""));
		user.addColumn(new Column("suUser", "SU_USER", "50", "varchar", false, ""));
		user.addColumn(new Column("suPhone", "SU_PHONE", "50", "varchar", false, ""));
		user.addColumn(new Column("suPwd", "SU_PWD", "50", "varchar", false, ""));
		user.addColumn(new Column("suEmail", "SU_EMAIL", "50", "varchar", false, ""));
		user.addColumn(new Column("suName", "SU_NAME", "50", "varchar", false, ""));
		user.addColumn(new Column("suAlipay", "SU_ALIPAY", "50", "varchar", false, ""));
		user.addColumn(new Column("suRm", "SU_RM", "20", "varchar", false, ""));
		user.addColumn(new Column("suRmcode", "SU_RMCODE", "20", "varchar", false, ""));
		user.addColumn(new Column("suPardpy", "SU_PARDPY", "20", "varchar", false, ""));
		user.addColumn(new Column("suIsdpy", "SU_ISDPY", "1", "BIT", false, ""));
		user.addColumn(new Column("suNote", "SU_NOTE", "1000", "varchar", false, ""));

		user.addColumn(new Column("recId", "REC_ID", "20", "varchar", true, ""));
		user.addColumn(new Column("enabled", "ENABLED", "1", "char", true, "1"));
		user.addColumn(new Column("recStatus", "REC_STATUS", "1", "char", true, "1"));
		user.addColumn(new Column("recPerson", "REC_PERSON", "20", "varchar", true, ""));
		user.addColumn(new Column("recTime", "REC_TIME", "", "TIMESTAMP", true, ""));
		user.addColumn(new Column("recUpdateperson", "REC_UPDATEPERSON", "20", "varchar", true, ""));
		user.addColumn(new Column("recUpdatetime", "REC_UPDATETIME", "", "TIMESTAMP", true, ""));

		// String alias, String name, String length, String type, boolean
		// isSys,String defValue
		return user;

	}

	@Override
	public String updatePwd(HashMap map) {
		// TODO Auto-generated method stub
		String result= "";
		String npwd="";
		SSysuser user= getUser(StrUtils.getNotNullStrValue(map.get("recId")));
		String oldPwd= StrUtils.getNotNullStrValue(map.get("suOldPwd"));
		// 判断密码是否一致
		MD5 md5 = new MD5();
		// 密码验证
		System.out.println(" user pwd:"+oldPwd);
		String intoPassword = md5.getMD5ofStr(user.getSuUser().toUpperCase() + oldPwd);
		if (!intoPassword.equals(user.getSuPwd())) {
			result = "-1";
		}else{
			// 判断前后密码是否一致
			String npwd1 = StrUtils.getNotNullStrValue(map.get("suNewPwd"));
			String npwd2 = StrUtils.getNotNullStrValue(map.get("suNewPwd1"));
			if(npwd1.equals(npwd2)){
				npwd=md5.getMD5ofStr(user.getSuUser().toUpperCase() + npwd1);
				SSysuser record = new SSysuser();
				record.setRecId( StrUtils.getNotNullStrValue(map.get("recId")));
				record.setSuPwd(npwd);
				
				dao.updateByPrimaryKeySelective(record);
				result ="1";
			}else{
				result ="-2";
			}
		}
		return result;
		
		
	}

}
